<!-- 栏目列表 -->
<template>
  <div class="DealerInfoStyle">
    <div class="cardAdmin">
      <div class="formContainer">
        <div style="margin: auto 0 auto auto;padding-bottom: 5px;padding-right: 10px;">
          <el-button disabled type="success" icon="el-icon-circle-plus-outline" size="small"
            @click="addbtn(0)">添加栏目</el-button>
        </div>
        <div style="position: absolute;left: 10px;bottom: 5px;">
        </div>
      </div>
    </div>
    <!-- 表格 -->
    <el-table :data="CompanyTable" row-key="category_id" border :row-style="tableRowStyle" class="x1919"
      :tree-props="{ children: 'childs', hasChildren: 'hasChildren' }">
      <el-table-column resizable prop="category_id" label="id" width="90" align="center">
      </el-table-column>
      <el-table-column resizable prop="category_level" label="类型" width="90" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.category_level + '级栏目' }}</span>
        </template>
      </el-table-column>
      <el-table-column resizable prop="category_type" label="链接类型" width="auto" min-width="120px" align="center">
        <template slot-scope="scope">
          <!-- 根据 category_type 映射显示不同的文案 -->
          <span v-if="scope.row.category_type == 1">新闻</span>
          <span v-if="scope.row.category_type == 2">研报</span>
          <span v-if="scope.row.category_type == 3">课程</span>
          <span v-if="scope.row.category_type == 4">跳转外部链接</span>
          <span v-if="!scope.row.category_type">未知类型</span>
        </template>
      </el-table-column>
      <el-table-column resizable prop="is_public" label="是否公开" width="auto" min-width="100px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.is_public ? '公开' : '不公开' }}</span>
        </template>
      </el-table-column>
      <el-table-column resizable prop="category_name_zh_tw" label="栏目名称（繁体中文）" width="auto" min-width="200px"
        align="center">
      </el-table-column>
      <!-- <el-table-column resizable prop="category_name_zh_cn" label="栏目名称（简体中文）" width="auto" min-width="200px"
        align="center">
      </el-table-column>
 
      <el-table-column resizable prop="category_name_en" label="栏目名称（英文）" width="auto" min-width="200px" align="center">
      </el-table-column>
      <el-table-column resizable prop="category_name_ja" label="栏目名称（日文)" width="auto" min-width="200px" align="center">
      </el-table-column>
      <el-table-column resizable prop="category_name_ko" label="栏目名称（韩文)" width="auto" min-width="200px" align="center">
      </el-table-column>

      <el-table-column resizable prop="link" label="链接" width="auto" min-width="200px" align="center">
      </el-table-column>
      <el-table-column resizable prop="category_name_ko" label="栏目名称（韩文)" width="auto" min-width="200px" align="center">
      </el-table-column> -->


      <el-table-column resizable label="操作" width="260" align="center">
        <template slot-scope="scope">
          <el-button v-if="scope.row.category_level < 3"
            @click="addbtn(scope.row.category_id, scope.row.category_level, scope.row.category_type)" type="success" plain
            size="small" disabled>添加子栏目</el-button>
          <el-button @click="tableEditing(scope.row)" type="primary" plain size="small">编辑</el-button>
          <el-button @click="del_columnDeletion(scope.row.category_id)" type="danger" plain size="small">刪除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗 -->
    <el-dialog :close-on-click-modal="false" :title="titles" :visible.sync="dialogVisible" width="70%"
      @close="handleClose" top="15vh">
      <div class="pop-up-form">
        <div style="display: flex;">

          <div class="form-options" style="">
            <span
              style="flex: 3; text-align: right; line-height: 40px; font-weight: 700; font-size: 15px;">繁体中文栏目名称：</span>
            <el-input style="flex: 6;" v-model="addform.category_name_zh_tw"></el-input>
          </div>
          <div class="form-options" style="">
            <span style="flex: 3; text-align: right; line-height: 40px; font-weight: 700; font-size: 15px;">简体栏目名称：</span>
            <el-input style="flex: 6;" v-model="addform.category_name_zh_cn"></el-input>
          </div>
        </div>

        <div style="display: flex;">

          <div class="form-options" style="">

            <span style="flex: 3; text-align: right; line-height: 40px; font-weight: 700; font-size: 15px;">日语栏目名称：</span>
            <el-input style="flex: 6;" v-model="addform.category_name_ja"></el-input>
          </div>
          <div class="form-options" style="">
            <span style="flex: 3; text-align: right; line-height: 40px; font-weight: 700; font-size: 15px;">英语栏目名称：</span>
            <el-input style="flex: 6;" v-model="addform.category_name_en"></el-input>
          </div>
        </div>
        <div style="display: flex;">
          <div class="form-options" style="">
            <span style="flex: 3; text-align: right; line-height: 40px; font-weight: 700; font-size: 15px;">韩语栏目名称：</span>
            <el-input style="flex: 6;" v-model="addform.category_name_ko"></el-input>
          </div>

        </div>
        <div style="display: flex;">
          <div class="form-options" style="">
            <span style="flex: 3; text-align: right; line-height: 40px; font-weight: 700; font-size: 15px;">栏目级别：</span>
            <el-select style="flex: 6;" v-model="addform.level" placeholder="栏目级别" disabled>
              <el-option v-for="item in level" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
          <div class="form-options" style="">
            <span style="flex: 3; text-align: right; line-height: 40px; font-weight: 700; font-size: 15px;">是否公开：</span>
            <el-switch style="flex: 6;height: 40px;" v-model="addform.is_public" active-text="是" inactive-text="否"
              :active-value="1" :inactive-value="0"></el-switch>
          </div>

        </div>
        <div style="display: flex;">
          <div class="form-options" style="">
            <span style="flex: 3; text-align: right; line-height: 40px; font-weight: 700; font-size: 15px;">类型：</span>
            <el-select style="flex: 6;" v-model="addform.category_type" placeholder="类型" @change="handleLanguageChange"
              :disabled="typeon">
              <el-option v-for="item in categorys" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
          <div class="form-options" style="">
            <span style="flex: 3; text-align: right; line-height: 40px; font-weight: 700; font-size: 15px;">栏目链接：</span>
            <el-input :disabled="linkon" style="flex: 6;" v-model="addform.link"></el-input>
          </div>

        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button v-show="titles == '新增'" type="primary" @click="add_columnAddition()" size="small">确
          定</el-button>
        <el-button v-show="titles == '编辑'" type="primary" @click="upd_topicalEditor()" size="small">更
          新</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<style>
a {
  color: #f0b546;
}
</style>
<script>
import { columnList, columnAddition, topicalEditor, columnDeletion } from "@/utils/api";
export default {
  name: 'CompanyInfo',
  data() {
    return {
      addform: {
        category_name_ja: '',
        category_name_ko: '',
        category_name_en: '',
        category_name_zh_tw: '',
        category_name_zh_cn: '',

        is_public: null,
        parent_id: 0,
        level: 1,
        category_type: 1,
        link: '',
      },
      CompanyTable: [],
      dialogVisible: false,
      titles: '',
      name: '',
      nametype: '编辑',
      linkon: true, // 链接输入框禁用
      typeon: true, // 类型输入框禁用

      level: [
        { value: 1, label: "一级" },
        { value: 2, label: "二级" },
        { value: 3, label: "三级" },
      ],
      categorys: [
        { value: 1, label: "新闻" },
        { value: 2, label: "研报" },
        { value: 3, label: "课程 " },
        { value: 4, label: "外部链接 " },
      ],

    };
  },
  mounted() {
    this.get_columnList()
  },

  methods: {
    tableRowStyle({ row }) {
      // 判断 category_level 并根据不同等级应用不同样式
      switch (row.category_level) {
        case 1:
          // 父级样式
          return {
            backgroundColor: '#ffffff',
            color: '#000',
            fontWeight: 500,
            fontSize: '14px !important',
            borderLeft: '5px solid #2196F3'  // 父级样式可以添加额外的样式
          };
        case 2:
          // 第二级样式
          return {
            backgroundColor: '#fdb398',
            color: '#000',
            fontWeight: 500,
            fontSize: '13px',
            borderLeft: '4px solid #2196F3'  // 子级样式的左侧边框颜色
          };
        case 3:
          // 第三级样式
          return {
            backgroundColor: '#f0b546',
            color: '#000',
            fontWeight: 500,
            fontSize: '12px',
            borderLeft: '3px solid #2196F3'  // 更细的边框
          };
        default:
          // 默认样式（如果没有 category_level 或者是其他情况）
          return {
            backgroundColor: '#ffffff',
            color: '#000',
            fontWeight: 500,
            fontSize: '14px !important',
          };
      }
    },
    handleLanguageChange(value) {
      if (value != 4 || value != 2) {
        this.linkon = true
        this.addform.link = ''
      } else {
        this.linkon = false
      }
    },
    // 查询栏目列表
    async get_columnList() {
      let data = {}
      const res = await columnList(data).catch((err) => {
        return console.error(err);
      });
      if (res.data && res.data.code == "200") {
        this.CompanyTable = res.data.data
        this.$message.success(res.data.message);

        this.total = res.data.total
      } else {

        this.$message.error(res.message);
      }
    },
    // 新增打开时
    addbtn(fid, level, type) {
      if (fid) {
        this.addform.parent_id = fid
      }
      if (level && level < 3) {
        this.typeon = true
        this.addform.level = level + 1
        this.addform.category_type = type
        if (type == 4 || type == 2) {
          this.linkon = false
        } else {
          this.linkon = true
        }
      }
      this.dialogVisible = true;
      this.titles = '新增'
    },
    // 新增
    async add_columnAddition() {
      let data = Object.assign({}, this.addform); // 浅拷贝

      // 删除空值键值对
      Object.keys(data).forEach(key => {
        if (data[key] === null || data[key] === undefined || data[key] === '') {
          delete data[key];
        }
      });
      const res = await columnAddition(data).catch((err) => {
        this.$message.error(err.response.data.message || '添加失败')
        return console.error(err);
      });
      if (res.data && res.data.code == "200") {
        this.$message.success(res.data.message);
        this.get_columnList()
        this.dialogVisible = false
      } else {
        this.$message.error(res.data.message);
      }
    },
    // 更新
    async upd_topicalEditor() {
      let data = Object.assign({}, this.addform); // 浅拷贝
      delete data.level
      delete data.category_type
      const res = await topicalEditor(data, this.addform.id).catch((err) => {
        return console.error(err);
      });
      if (res.data && res.data.code == "200") {
        this.$message.success(res.data.message);
        this.dialogVisible = false
        this.currentPage = 1;
        this.get_columnList()
      } else {
        this.$message.error(res.data.msg);
      }
    },
    // 关闭弹窗时
    handleClose() {
      this.addform = {
        category_name_ja: '',
        category_name_ko: '',
        category_name_en: '',
        category_name_zh_tw: '',
        category_name_zh_cn: '',
        is_public: null,
        parent_id: 0,
        level: 1,
        category_type: 1,
        link: '',

      }
      this.typeon = false
    },
    handleClosetable() {

    },

    // 分页
    current_change(currentPage) {
      this.currentPage = currentPage;
      this.get_columnList();
    },
    // 刪除
    async del_columnDeletion(id) {
      try {
        await this.$confirm("是否继续执行操作？", "提示", {
          confirmButtonText: "是",
          cancelButtonText: "否",
          type: "warning",
        });

        const res = await columnDeletion(id).catch((err) => {
          return console.error(err);
        });

        if (res.data && res.data.code == "200") {
          this.$message.success(res.data.msg);
          this.dialogVisible = false;
          this.get_columnList();
        } else {
          this.$message.error(res.data.msg);
        }
      } catch (error) {
        // 用户点击取消或关闭对话框
        console.error(error);
      }
    },
    // 重置
    resetForm() {
      this.currentPage = 1
      this.get_columnList();
    },
    // 编辑打开时
    tableEditing(row) {
      this.titles = '编辑'
      this.dialogVisible = true
      this.typeon = true
      // row.father_menu_id == 0 ? this.nametype = '主菜单' : this.nametype = '子菜单'
      const isTrue = (value) => {
        if (value == null || value == undefined || value === "null" || value === "undefined") {
          return false;
        }
        return true;
      };
      if (isTrue(row.category_name_en)) {
        this.addform.category_name_en = row.category_name_en
      }
      if (isTrue(row.category_name_ja)) {
        this.addform.category_name_ja = row.category_name_ja
      }
      if (isTrue(row.category_name_zh_cn)) {
        this.addform.category_name_zh_cn = row.category_name_zh_cn
      }

      if (isTrue(row.category_name_ko)) {
        this.addform.category_name_ko = row.category_name_ko
      }
      if (isTrue(row.category_name_zh_tw)) {
        this.addform.category_name_zh_tw = row.category_name_zh_tw
      }
      if (isTrue(row.parent_id)) {
        this.addform.parent_id = row.parent_id
      }
      if (isTrue(row.is_public)) {
        this.addform.is_public = row.is_public
      }
      if (isTrue(row.link)) {
        this.addform.link = row.link
      }
      if (isTrue(row.category_level)) {
        this.addform.level = row.category_level
      }
      if (isTrue(row.category_type)) {
        this.addform.category_type = row.category_type
      }


      if (row.category_type == 4 || row.category_type == 2) {
        this.linkon = false
      } else {
        this.linkon = true
        this.addform.link = row.link
      }

      this.addform.id = row.category_id
    },
  },
};
</script>
<style>
.cell {
  font-size: 12px;
  white-space: nowrap !important;
}

.el-dialog__body {
  padding: 0 10px !important;
}

.demo-table-expand {
  font-size: 0;
  margin-left: 10px;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
<style lang="less" scoped>
.el-textarea {
  overflow: hidden;
  /* 隐藏滚动条 */
}

.DealerInfoStyle {
  width: 100%;
  padding: 0 10px;
  // background-color: aquamarine;
  height: calc(100vh - 100px);
  // border: 1px #000 solid;

  .cardAdmin {
    position: relative;
    // height: 18%;
    width: 100%;
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    margin: 0;

    border: #ebeef5 solid 1px;
    padding: 0 5px;
    margin-bottom: 2px;


    .formContainer {
      width: 100%;
      // display: flex;
      flex-wrap: wrap;
      padding: 0;
      align-self: baseline;
      margin: 10px 0;
    }

    .formSearch {
      width: 25%;
      min-width: 25%;
      margin: 10px 0;
      height: 40px;
      display: flex;
      justify-content: center;
    }
  }

  .pop-up-form {
    width: 100%;
    padding: 0;
    margin: 10px 0;
    padding-right: 15px;

    .form-options {
      width: 50%;
      min-width: 50%;
      height: 40px;
      display: flex;
      justify-content: center;
      margin: 10px 0;
    }
  }
}
</style>
<style>
.cell {
  font-size: 12px;
  white-space: nowrap !important;
}

textarea {
  resize: none !important;
}
</style>